
package com.x52im.rbchat.logic.chat_root.impl;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.FrameLayout;

import com.eva.android.widget.SimpleGridView;
import com.eva.android.widget.SimpleViewPager;
import com.eva.android.widget.WidgetUtils;
import com.eva.epc.common.util.CommonUtils;
import com.x52im.rbchat.Const;
import com.x52im.rbchat.R;

import java.util.ArrayList;

public abstract class AbstractMoreUIWrapper extends SimpleViewPager
{
    protected Activity parentActivity = null;
    /** 主Activity的layout预留给本布局的父布局 */
    protected FrameLayout layoutbottomContentOfParent = null;

    public  AbstractMoreUIWrapper(final Activity context, final FrameLayout layoutbottomContent)
    {
        super(context
                , R.layout.chatting_plus_functions_viewpager
                , R.id.chatting_plus_functions_viewpager_layoutBottom
                , R.id.chatting_plus_functions_viewpager_viewPager);

        this.parentActivity = context;
        this.layoutbottomContentOfParent = layoutbottomContent;

        refreshUI();

        // 加入到输入框下方的更多功能父布局中
        this.layoutbottomContentOfParent.addView(this, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT ));
    }

    /**
     * 界面显示的核心实现方法.
     *
     * @see #putDatas(ArrayList, ArrayList)
     */
    public void refreshUI()
    {
        // 每页显示总数
        int num_per_page = Const.MORE_FUNCTIONS_PAGER_EVERY_PAGE_COLUMNS * Const.MORE_FUNCTIONS_PAGER_EVERY_PAGE_LINES;
        // 当开发者配置出了非法值时，给个默认值
        if(num_per_page <= 0)
            num_per_page = 8;

        // 要显示的所有礼品集合
        ArrayList<SimpleGridView.DefaultElementDTO> listDatas = createChatFunctionsData();

        // 提交新数据并刷新ui显示
        this.putDatas(getViews(parentActivity, listDatas, num_per_page)
                , getPagerIndicators(parentActivity, listDatas.size(), num_per_page));
    }

    protected abstract ArrayList<SimpleGridView.DefaultElementDTO> createChatFunctionsData();

    // 返回的界面上的页面ui集合
    protected ArrayList<View> getViews(Activity context, ArrayList<SimpleGridView.DefaultElementDTO> listDatas, int num_per_page)
    {
        ArrayList<View> views = new ArrayList<View>();

        // 遍历所有元素（并自动按定义的每页显示数进行分页）
        ArrayList<SimpleGridView.DefaultElementDTO> childListDatas = null;
        for(int i = 0; i < listDatas.size();i++)
        {
            // 当整页时，进行分页处理
            if(i % num_per_page == 0)
            {
                // 当childListDatas == null时是i==0时的特殊情况，注意一下！
                if(childListDatas != null)
                    views.add(createGridViewObj(context, childListDatas));

                childListDatas = new ArrayList<SimpleGridView.DefaultElementDTO>();
            }
            childListDatas.add(listDatas.get(i));

            // 特殊情况处理：上述算法中最后一页是无法加入到views中的，所以要特殊处理哦
            if(i == (listDatas.size() - 1))
                views.add(createGridViewObj(context, childListDatas));
        }
        return views;
    }

    // 返回界面上的指示器ui组件
    protected ArrayList<PaggerIndicator> getPagerIndicators(final Activity context, int allFaceCount, int num_per_page)
    {
        ArrayList<PaggerIndicator> ids = new ArrayList<PaggerIndicator>();

        // 指示器个数 = 无条件进位【总表情集数 / 每页数】的结果（即2.1=3页，2=2页，2.9=3页）
        int indicatorCount = CommonUtils.getLikePageNum(allFaceCount, num_per_page);

        // 按个数建立指示器ui组件
        for(int i = 0; i < indicatorCount; i++) {
            DefaultPaggerIndicatorImageView pi = new DefaultPaggerIndicatorImageView(context) {
                public void setIndicatorSelected(boolean selected) {
                    if (selected)
                        this.setImageResource(R.drawable.chatting_morefunc_pager_indicator_selected_icon);
                    else
                        this.setImageResource(R.drawable.chatting_morefunc_pager_indicator_unselected_icon);
                }
            };

            pi.setPadding(WidgetUtils.dip2px(context, 5.0f)
                    , WidgetUtils.dip2px(context, 0.0f)
                    , WidgetUtils.dip2px(context, 5.0f)
                    , WidgetUtils.dip2px(context, 0.0f));

            ids.add(pi);
        }

        return ids;
    }

    // 根据传进来的每页元素数据集，生成该页的GridView对象
    protected SimpleGridView createGridViewObj(Activity context, ArrayList<SimpleGridView.DefaultElementDTO> childListDatas)
    {
        // 实例化gridview
        SimpleGridView thisGridView = new SimpleGridView(context
                , R.layout.chatting_plus_functions_gridview, R.id.chatting_plus_functions_gridview_id
                , R.layout.chatting_plus_functions_gridview_item, R.id.chatting_plus_functions_gridview_item_nameView, R.id.chatting_plus_functions_gridview_item_imageView
                , childListDatas);
        // 去掉gridview默认的选样式
        thisGridView.getGridview().setSelector(new ColorDrawable(Color.TRANSPARENT));
        // 每页列数设置
        thisGridView.getGridview().setNumColumns(Const.MORE_FUNCTIONS_PAGER_EVERY_PAGE_COLUMNS);

        return thisGridView;
    }

    protected SimpleGridView.DefaultItemAction createChatFunctionsAction(final Activity context, final int actionId)
    {
        return new SimpleGridView.DefaultItemAction(actionId){
            @Override
            public void actionPerformed(Object obj)
            {
                fireChatFunctionsAction((int)actionId);
            }
        };
    }

    protected abstract void fireChatFunctionsAction(int actionId);

    public void auto()
    {
        if(this.isShowing())
            this.hide();
        else
            this.show();
    }

    public void show()
    {
        this.layoutbottomContentOfParent.setVisibility(View.VISIBLE);
    }

    public void hide()
    {
        this.layoutbottomContentOfParent.setVisibility(View.GONE);
    }

    public boolean isShowing()
    {
        return (this.layoutbottomContentOfParent.getVisibility() == View.VISIBLE);
    }
}
